﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI;
using Doo.Controls;

namespace Doo.Scripts.Fields
{
    public class FieldCollection : List<Field>
    {
        public Field this[string name]
        {
            get
            {
                return this.FirstOrDefault(r => r.Name == name);
            }
        }

        protected ScriptObject _ScriptObject;

        public void Init(ScriptObject so)
        {
            this._ScriptObject = so;

            foreach (var field in this)
            {
                field.Init(so);
            }
        }
    }

    public abstract class Field
    {
        protected Doo.Controls.ScriptObject _so;

        /// <summary>
        /// 归属的对象
        /// </summary>
        public Doo.Controls.ScriptObject ScriptObject { get { return _so; } }

        public void Init(Doo.Controls.ScriptObject so)
        {
            this._so = so;
        }

        /// <summary>
        /// 字段名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 是否加引号
        /// </summary>
        public virtual bool NoQuote { get { return true; } }

        /// <summary>
        /// 根据环境来绘制名
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="parent"></param>
        public virtual void RenderName(TextWriter writer, Control parent)
        {
            writer.Write(Name);
        }

        /// <summary>
        /// 根据环境来绘制值
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="parent"></param>
        public abstract void RenderValue(TextWriter writer, Control parent);
    }
}
